/**
 * Created by gongxun on 17/1/19.
 */

var PszTableSettingLayer = GameCCSBase.extend({
    Slider_effect : null,
    Slider_music : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/PszTableSetting.json");

        //button
        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
            }
        }, this);
        mo.seekWidgetByName(this.ccsNode, "Panel_touch").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
            }
        }, this);

        //退出
        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
                if (mo.pszTable.mPlayerType == 0){
                    // mo.pszTable.mNnReplayLayer.onPlayEnd();
                }else{
                    if ((mo.pszTableStatus.mTableStatus.LeftPlayTimes == mo.pszTable.mGameConfig.maxpan) &&
                        mo.pszTableStatus.mTableStatus.Step == 0){
                        mo.gameMsgManager.sendForceExitTableRequestpsz();
                    }else{
                        mo.pszTable.mKwxApplyTableLayer.sendCancelTableAgree();
                    }
                }
            }
        }, this);

        this.Slider_effect = mo.seekWidgetByName(this.ccsNode, "Slider_effect");
        this.Slider_music = mo.seekWidgetByName(this.ccsNode, "Slider_music");
        this.Slider_effect.addEventListener(this.sliderEvent, this);
        this.Slider_music.addEventListener(this.sliderEvent, this);

        //effects
        var Panel_effect = mo.seekWidgetByName(this.ccsNode, "Panel_effect");
        mo.seekWidgetByName(Panel_effect, "Image_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicAndEffects(mo.gameUserInfo.mMusicVolume, 0);
            }
        }, this);

        mo.seekWidgetByName(Panel_effect, "Image_open").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicAndEffects(mo.gameUserInfo.mMusicVolume, 100);
            }
        }, this);

        //music
        var Panel_music = mo.seekWidgetByName(this.ccsNode, "Panel_music");
        mo.seekWidgetByName(Panel_music, "Image_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicAndEffects(0, mo.gameUserInfo.mEffectsVolume);
            }
        }, this);

        mo.seekWidgetByName(Panel_music, "Image_open").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicAndEffects(100, mo.gameUserInfo.mEffectsVolume);
            }
        }, this);

        this.setVisible(false);
    },

    onEnter: function () {
        this._super();

        //音乐
        this.updateMusicAndEffets(mo.gameUserInfo.mMusicVolume, mo.gameUserInfo.mEffectsVolume);
        mo.eventHelper.add("updateMusicAndEffects", this, function () {
            this.updateMusicAndEffets(mo.gameUserInfo.mMusicVolume, mo.gameUserInfo.mEffectsVolume);
        });
    },

    onExit: function () {
        mo.eventHelper.remove(this);

        this._super();
    },

    updateMusicAndEffets: function (musicVolume, effectsVolume) {

        mo.seekWidgetByName(this.ccsNode, "Slider_music").setPercent(musicVolume);

        mo.seekWidgetByName(this.ccsNode, "Slider_effect").setPercent(effectsVolume);
    },

    sliderEvent: function (sender, type) {
        switch (type) {
            case ccui.Slider.EVENT_PERCENT_CHANGED:
                if (this.Slider_effect == sender) {
                    var slider = sender;
                    var percent = slider.getPercent();
                    percent = parseInt(percent);
                    mo.soundHelper.setEffectsVolume(percent);
                } else {
                    var slider = sender;
                    var percent = slider.getPercent();
                    percent = parseInt(percent);
                    mo.soundHelper.setMusicVolume(percent);
                }
                break;
            default:
                break;
        }
    },

    setDialog: function (isVisible) {
        if (isVisible) {
            this.setVisible(true);
            this.showAction(mo.seekWidgetByName(this.ccsNode, "Panel_center"));
        } else {
            this.closeAction(mo.seekWidgetByName(this.ccsNode, "Panel_center"));
            mo.gameUserInfo.setMusicAndEffects(parseInt(this.Slider_music.getPercent()), parseInt(this.Slider_effect.getPercent()));

        }
    }
});